<%@page pageEncoding="UTF-8" %>
<!doctype html>
<html lang="zh-cmn">
<head>
    <title>Document</title>
    <%@include file="/WEB-INF/jsp/header.jsp"%>
</head>
<body>
<div class="container-fluid">

    <div class="panel panel-info">
        <div class="panel-heading">
            <h3 class="panel-title">条件查询</h3>
        </div>
        <div class="panel-body">
            <form action="" class="form-inline" method="post">
                <div class="form-group">
                    <label for="">
                        药品名称:
                    </label>
                    <input type="text" class="form-control" name="drugName" />
                </div>
                <button type="button" onclick="searchInsufficient()" class="btn btn-info">搜索</button>
            </form>
        </div>
    </div>
    <div id="toolbar">
        <button type="button" data-content="批量补货，只能补货一定量!" data-original-title="批量进行补货" class="layui-btn layui-btn-small" id="add" onclick="replenishments()">
            <i class="layui-icon"></i> 批量补货
        </button>
    </div>
    <table
            id="drugstockInsufficient"
            data-toggle="table"
            data-url="${ctx}/stock/searchInsufficient"
            data-toolbar="#toolbar"
            data-striped="true"
            data-pagination="true"
            data-side-pagination="server"
            data-page-list="[5,10,15,30]"
            data-smart-display="true"
    >
        <thead>
            <tr>
                <th data-checkbox="true"></th>
                <th data-field="druginfo.drugName">药品名称</th>
                <th data-field="druginfo.drugStandard">药品规格</th>
                <th data-field="druginfo.drugUnit">药品单位</th>
                <th data-field="druginfo.drugPreparation">药品剂型</th>
                <th data-field="stoPcs">库存数量</th>
                <th data-field="druginfo.drugFloor">库存下线</th>
                <th data-formatter="optEvent" data-events="optEvents">操作</th>
            </tr>
        </thead>
    </table>
</div>
<div id="batchReplenishment">
    <form action="" class="form-horizontal">

    </form>
</div>
<div id="addReplenishment">
    <form class="form-horizontal">
        <div class="form-group dan">
            <label for="" class="col-md-3">
                补货商品:
            </label>
            <div class="col-md-8">
                <select name="drugNum" id="drugName" class="form-control">

                </select>
            </div>
        </div>
        <div class="form-group dan">
            <label for="" class="col-md-3">
                现有数量:
            </label>
            <div class="col-md-8">
                <input type="hidden" name="stoPrimecost" />
                <input type="hidden" name="stoPresell" />
                <input type="hidden" name="stoPcs" />
                <input type="hidden" name="drugName" />
                <input type="text" disabled="disabled" id="stoPcs" name="stoPcs" class="form-control" />
            </div>
        </div>
        <div class="form-group" id="shear">
            <label for="" class="col-md-3">
                补货数量:
            </label>
            <div class="col-md-8">
                <input type="number" name="delBuynum" class="form-control" />
            </div>
        </div>
    </form>
</div>

<%@include file="/WEB-INF/jsp/footer.jsp"%>
<script src="${ctx}/statics/js/bootstrap-tooltip.js"></script>
<script src="${ctx}/statics/js/bootstrap-popover.js"></script>

<script>
    $("#add").popover();
    function optEvent() {
        return '<button type="button" class="layui-btn layui-btn-danger layui-btn-mini replenishment">补货</button>';
    }
    

    //单个补货
    function singleReplenishment(row) {
        $("#addReplenishment").dialog("open");
        $("#addReplenishment form").setForm(row)
    }










    window.optEvents = {
        "click .replenishment":function (e,value,row) {
            $("[name=drugName]").val(row.druginfo.drugName)
            singleReplenishment(row)
        }
    }

    //批量补货
    function replenishments() {
        var arr = $("#drugstockInsufficient").bootstrapTable("getSelections");
        if(arr.length==0){
            swal({
                title:"提示",
                text:"请至少选择一项进行补货!"
            })
            return;
        }

        $("#shear").clone().appendTo("#batchReplenishment form")
        $("#batchReplenishment").dialog("open");

    }
    //条件查询
    function searchInsufficient() {
        var param = {};
        param.drugName=$("[name=drugName]").val();
        $("#drugstockInsufficient").bootstrapTable("refresh",{query:param})
    }
    //补货方法
    function replenishment() {
        $("#addReplenishment").dialog({
            title:"库存补货",
            btnValue:"补货",
            addBind:function () {
                ClickBuHuo();
            }
        })
    }
    //批量补货
    function batchReplenishment() {
        $("#batchReplenishment").dialog({
            title:"批量补货",
            btnValue:"补货",
            addBind:function () {
                ClickBacthBuHuo();
            }
        })
    }
    //批量补货
    function ClickBacthBuHuo() {
        var bh = $("[name=delBuynum]").val();
        if(bh=='' || bh==null){
            swal({
                title:"提示",
                text:"请填写补货数量！"
            })
            return;
        }
        if(bh<=0){
            swal({
                title:"提示",
                text:"请填写正确的补货数量！"
            })
            return;
        }
        var arr = $("#drugstockInsufficient").bootstrapTable("getSelections");
        var param = $(arr).map(function () {
            return "stoNum="+this.stoNum;
        }).get().join("&");
        console.log(param)
        location.href=ctx+"/stock/batchAddStock?"+param+"&bh="+bh;
    }
    //单个补货
    function ClickBuHuo() {
        var bh = $("[name=delBuynum]").val();
        if(bh=='' || bh==null){
            swal({
                title:"提示",
                text:"请填写补货数量！"
            })
            return;
        }
        if(bh<=0){
            swal({
                title:"提示",
                text:"请填写正确的补货数量！"
            })
            return;
        }
         var param = $("#addReplenishment form").serialize();
        location.href=ctx+"/stock/addStock?"+param;
    }

    //药品名称方法
    function initInsufficientDrugnfo() {
        $.post(ctx+"/stock/searchInsufficientNofen",null,function (arr) {
            $.each(arr,function () {
                var $opt = $("<option/>")
                $opt.val(this.druginfo.drugNum)
                $opt.html(this.druginfo.drugName)
                $opt.appendTo("#drugName")
            })
        },'json')
    };
    //下拉宽改变
    function initchangeInsufficient() {
        $("#drugName").change(function () {
            var drugNum = $(this).val();
            $.post(ctx+"/stock/searchInsufficientNofen","drugNum="+drugNum,function (msg) {
                $("[name=stoPcs]").val(msg[0].stoPcs)
                $("[name=drugName]").val(msg[0].druginfo.drugName)
            },'json')
        })
    }
    //实例化方法
    $(function () {
        replenishment();
        initInsufficientDrugnfo();
        initchangeInsufficient();
        batchReplenishment();
    })
</script>
</body>
</html>
